Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / ui / jucer_EditingPanelBase.cpp
blob557de8cf5b0773b10345631fe7b40c277b134738
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_EditingPanelBase.h"
28 #include "jucer_JucerDocumentHolder.h"
30 //==============================================================================
31 class EditingPanelBase::MagnifierComponent : public Component
33 public:
34 MagnifierComponent (Component* content_)
35 : scaleFactor (1.0), content (content_)
37 addAndMakeVisible (content);
38 childBoundsChanged (content);
41 void childBoundsChanged (Component* child)
43 const Rectangle<int> childArea (getLocalArea (child, child->getLocalBounds()));
44 setSize (childArea.getWidth(), childArea.getHeight());
47 double getScaleFactor() const { return scaleFactor; }
49 void setScaleFactor (double newScale)
51 scaleFactor = newScale;
52 content->setTransform (AffineTransform::scale ((float) scaleFactor,
53 (float) scaleFactor));
56 private:
57 double scaleFactor;
58 ScopedPointer<Component> content;
61 //==============================================================================
62 class ZoomingViewport : public Viewport
64 public:
65 ZoomingViewport (EditingPanelBase* const panel_)
66 : panel (panel_),
67 isSpaceDown (false)
71 ~ZoomingViewport() {}
73 void mouseWheelMove (const MouseEvent& e, float ix, float iy)
75 if (e.mods.isCtrlDown() || e.mods.isAltDown())
77 const double factor = (iy > 0) ? 2.0 : 0.5;
79 panel->setZoom (panel->getZoom() * factor, e.x, e.y);
81 else
83 Viewport::mouseWheelMove (e, ix, iy);
87 void dragKeyHeldDown (const bool isKeyDown)
89 if (isSpaceDown != isKeyDown)
91 isSpaceDown = isKeyDown;
93 if (isSpaceDown)
95 DraggerOverlayComp* const dc = new DraggerOverlayComp();
96 addAndMakeVisible (dc);
97 dc->setBounds (0, 0, getWidth(), getHeight());
99 else
101 for (int i = getNumChildComponents(); --i >= 0;)
103 if (dynamic_cast <DraggerOverlayComp*> (getChildComponent (i)) != 0)
105 delete getChildComponent (i);
112 private:
113 EditingPanelBase* const panel;
114 bool isSpaceDown;
116 //==============================================================================
117 class DraggerOverlayComp : public Component
119 public:
120 DraggerOverlayComp()
122 setMouseCursor (MouseCursor::DraggingHandCursor);
123 setAlwaysOnTop (true);
126 ~DraggerOverlayComp()
130 void mouseDown (const MouseEvent& e)
132 Viewport* viewport = findParentComponentOfClass ((Viewport*) 0);
134 if (viewport != 0)
136 startX = viewport->getViewPositionX();
137 startY = viewport->getViewPositionY();
141 void mouseDrag (const MouseEvent& e)
143 Viewport* viewport = findParentComponentOfClass ((Viewport*) 0);
145 if (viewport != 0)
147 viewport->setViewPosition (jlimit (0, jmax (0, viewport->getViewedComponent()->getWidth() - viewport->getViewWidth()),
148 startX - e.getDistanceFromDragStartX()),
149 jlimit (0, jmax (0, viewport->getViewedComponent()->getHeight() - viewport->getViewHeight()),
150 startY - e.getDistanceFromDragStartY()));
154 private:
155 int startX, startY;
160 //==============================================================================
161 EditingPanelBase::EditingPanelBase (JucerDocument& document_,
162 Component* propsPanel_,
163 Component* editorComp)
164 : document (document_),
165 editor (editorComp),
166 propsPanel (propsPanel_)
168 addAndMakeVisible (viewport = new ZoomingViewport (this));
169 addAndMakeVisible (propsPanel);
171 viewport->setViewedComponent (magnifier = new MagnifierComponent (editor));
174 EditingPanelBase::~EditingPanelBase()
176 deleteAllChildren();
179 void EditingPanelBase::resized()
181 const int contentW = jmax (1, getWidth() - 260);
183 propsPanel->setBounds (contentW + 4, 4, jmax (100, getWidth() - contentW - 8), getHeight() - 8);
185 viewport->setBounds (4, 4, contentW - 8, getHeight() - 8);
187 if (document.isFixedSize())
188 editor->setSize (jmax (document.getInitialWidth(),
189 roundToInt ((viewport->getWidth() - viewport->getScrollBarThickness()) / getZoom())),
190 jmax (document.getInitialHeight(),
191 roundToInt ((viewport->getHeight() - viewport->getScrollBarThickness()) / getZoom())));
192 else
193 editor->setSize (viewport->getWidth(),
194 viewport->getHeight());
197 void EditingPanelBase::visibilityChanged()
199 if (isVisible())
201 updatePropertiesList();
203 if (getParentComponent() != 0)
205 resized();
206 JucerDocumentHolder* const cdh = dynamic_cast <JucerDocumentHolder*> (getParentComponent()->getParentComponent());
208 if (cdh != 0)
209 cdh->setViewportToLastPos (viewport, *this);
211 resized();
214 else
216 if (getParentComponent() != 0)
218 JucerDocumentHolder* const cdh = dynamic_cast <JucerDocumentHolder*> (getParentComponent()->getParentComponent());
220 if (cdh != 0)
221 cdh->storeLastViewportPos (viewport, *this);
225 editor->setVisible (isVisible());
228 double EditingPanelBase::getZoom() const
230 return magnifier->getScaleFactor();
233 void EditingPanelBase::setZoom (double newScale)
235 setZoom (jlimit (1.0 / 8.0, 16.0, newScale),
236 viewport->getWidth() / 2,
237 viewport->getHeight() / 2);
240 void EditingPanelBase::setZoom (double newScale, int anchorX, int anchorY)
242 Point<int> anchor (editor->getLocalPoint (viewport, Point<int> (anchorX, anchorY)));
244 magnifier->setScaleFactor (newScale);
246 resized();
247 anchor = viewport->getLocalPoint (editor, anchor);
249 viewport->setViewPosition (jlimit (0, jmax (0, viewport->getViewedComponent()->getWidth() - viewport->getViewWidth()),
250 viewport->getViewPositionX() + anchor.getX() - anchorX),
251 jlimit (0, jmax (0, viewport->getViewedComponent()->getHeight() - viewport->getViewHeight()),
252 viewport->getViewPositionY() + anchor.getY() - anchorY));
255 void EditingPanelBase::xyToTargetXY (int& x, int& y) const
257 Point<int> pos (editor->getLocalPoint (this, Point<int> (x, y)));
258 x = pos.getX();
259 y = pos.getY();
262 void EditingPanelBase::dragKeyHeldDown (bool isKeyDown)
264 ((ZoomingViewport*) viewport)->dragKeyHeldDown (isKeyDown);